﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;

public partial class System_RolePermissions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ActionTitle.Text = Role.GetRole(Request.QueryString["RoleID"]).RoleName + "权限设置";
            BindPermissionCategoryList();
            BindPermissionList();
            BindRolePermissionList();
        }
    }
    private void BindPermissionCategoryList()
    {
        List<PermissionCategoryInfo> categories = PermissionCategory.GetList("");
        ListItem li;
        foreach (PermissionCategoryInfo category in categories)
        {
            li = new ListItem();
            li.Text = category.CategoryName;
            li.Value = category.CategoryID;
            PermissionCategoryList.Items.Add(li);
        }
    }
    private void BindPermissionList()
    {
        PermissionList.Items.Clear();
        List<PermissionInfo> permissions = Permission.GetList("CategoryID='" + PermissionCategoryList.SelectedValue + "'");
        List<PermissionInfo> rolePermissions = Role.GetRolePermissionList(Request.QueryString["RoleID"]);
        ListItem li;
        foreach (PermissionInfo permission in permissions)
        {
            if (!rolePermissions.Contains(permission))
            {
                li = new ListItem();
                li.Text = permission.PermissionName;
                li.Value = permission.PermissionID;
                PermissionList.Items.Add(li);
            }
        }
    }
    private void BindRolePermissionList()
    {
        RolePermissionList.Items.Clear();
        List<PermissionInfo> permissions = Role.GetRolePermissionList(Request.QueryString["RoleID"]);
        ListItem li;
        foreach (PermissionInfo permission in permissions)
        {
            li = new ListItem();
            li.Text = permission.PermissionName;
            li.Value = permission.PermissionID;
            RolePermissionList.Items.Add(li);
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        if (PermissionList.SelectedValue != "")
        {
            foreach (ListItem li in PermissionList.Items)
            {
                if (li.Selected)
                    Role.AddPermission(Request.QueryString["RoleID"], li.Value);
            }
            BindPermissionList();
            BindRolePermissionList();
        }
        else
            Prompt.Text = "警告：请选择一个权限！";
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        if (RolePermissionList.SelectedValue != "")
        {
            foreach (ListItem li in RolePermissionList.Items)
            {
                if (li.Selected)
                    Role.RemovePermission(Request.QueryString["RoleID"], li.Value);
            }
            BindPermissionList();
            BindRolePermissionList();
        }
        else
            Prompt.Text = "警告：请选择一个权限！";
    }
    protected void PermissionCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindPermissionList();
    }
}
